package adv47;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author oldliu
 * @since 1.0
 */
public class ObjectServer {
    public static void main(String[] args) {
        ExecutorService es = Executors.newCachedThreadPool();
        try {
            ServerSocket st=new ServerSocket(9999);
            while (true){
               final Socket s = st.accept();
               es.execute(new ObjectThread(s));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
class ObjectThread implements Runnable{
    private final Socket s;
    public static Map<String,String> map=new HashMap<>();
    static {
        map.put("最高","来勇");
        map.put("最帅","常以名");
        map.put("最矮","力帆");
        map.put("最胖","镍锰开");
        map.put("最瘦","胡汪洋");
    }
    public ObjectThread(Socket s){
        this.s=s;
    }
    @Override
    public void run() {
        try {
            System.out.println(s+"连接了");
            InputStream in = s.getInputStream();
            OutputStream out = s.getOutputStream();
            ObjectOutputStream oout=new ObjectOutputStream(out);
            ObjectInputStream oin=new ObjectInputStream(in);
            oout.writeObject(map);
            oout.flush();
            Object obj = oin.readObject();
            System.out.println("从"+s+"收到了"+obj);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}